import java.util.*;

/**
 * @author wangj881
 * @date 2024-12-24 10:21
 */
public class L49_3 {

    public static void main(String[] args) {
        String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
        List<List<String>> list = new Solution().groupAnagrams(strs);
        for (List<String> strings : list) {
            System.out.println(strings.toString());
        }

    }

    static class Solution {
        public List<List<String>> groupAnagrams(String[] strs) {
            ArrayList<List<String>> result = new ArrayList<>();
            HashMap<String, List<String>> map = new HashMap<>();
            int[] ints = new int[26];
            for (String str : strs) {
                for (int i = 0; i < str.length(); i++) {
                    int index = str.charAt(i) - 'a';
                    ints[index]++;
                }
                String key = Arrays.toString(ints);
                List<String> orDefault = map.getOrDefault(key, new ArrayList<>());
                orDefault.add(str);
                if (orDefault.size() == 1) {
                    map.put(key, orDefault);
                }
                Arrays.fill(ints, 0);
            }
            result.addAll(map.values());
            return result;
        }
    }
}
